﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel.Channels;
using System.Diagnostics;
using HomeFramework.Model;
using System.IO;
using System.Xml;

namespace HomeFramework.Services
{
    partial class HomeFrameworkEngine
    {
        internal class HelpPageGenerator
        {
            public static Message GenerateHelpPage(string name, string version, Message reply, string baseAddress)
            {
                Log.WriteLine(Log.SourceEnum.WsdlGenerator, TraceEventType.Verbose, "Help page generator working for {0}", name);

                using (var ts = new TransactionScript())
                {
                    var h = ts.FindByName(name, version);
                    if (h != null)
                    {
                        var html = HomeFramework.Properties.Resources.HelpPage;
                        var path = baseAddress + "?wsdl=" + name + (!string.IsNullOrEmpty(version) ? "&version=" + version : "");
                        html = html.Replace("{0}", name).Replace("{1}", HttpUtility.HtmlEncode(path));

                        var r = XmlReader.Create(new StringReader(html));
                        return CloneEnvelope(reply, r);
                    }
                    else
                        return null;
                }
            }
        }
    }
}